package top.chj.day17.exam.test;
import java.text.Collator;
import java.util.Arrays;
import java.util.Locale;
/**
 * @Auther:小陈的Java
 * 定义User类，类中有属性id,name,age ；通过自然排序&定制排序两种方式实现User数组的按照年龄排序，
 * 如果年龄一样，按照姓名排序（提示：汉语拼音依赖从网上maven仓库获取，用法百度搜索）
 */
class User implements Comparable{
    private int id;
    private String name;
    private int age;

    public User(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public User() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public static void main(String[] args) {
        User[] users = new User[3];
        users[0] = new User(1,"张三",19);
        users[1] = new User(1,"李四",18);
        users[2] = new User(1,"王五",17);
        Arrays.sort(users);
        System.out.println(Arrays.toString(users));
    }

    @Override
    public int compareTo(Object o) {
        if(o==null){
            throw  new RuntimeException("null不能参与排序");
        }
        if(!(o instanceof User)){
            throw  new ClassCastException("不是User类型不支持排序");
        }
        User user = (User)o;
        if(this.age!=user.getAge()){
            return this.age-user.getAge();
        }else {
            Collator collator = Collator.getInstance(Locale.CHINA);
            return collator.compare(this.name,user.getName());
        }
    }
}